package com.djsearch.data;

import java.util.ArrayList;
import java.util.List;

import com.djsearch.bean.LoginSuccess;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


/**
 * 该类包含对数据库中的表进行增 ，查功能方法
 * 
 * @author chenyang
 * 
 */

public class OperateSQLiteDataBase {
	private Context context;
	private DbHelper dbHelper;
	private static int key = 1;
	private static OperateSQLiteDataBase operateSQLiteDataBase = null;
	private SharedPreferences preferences;

	/**
	 * 获取该类对象，单例
	 * 
	 * @param context
	 * @return
	 */

	public static OperateSQLiteDataBase getOperateSQLiteDataBase(Context context) {
		if (operateSQLiteDataBase == null) {
			operateSQLiteDataBase = new OperateSQLiteDataBase();
		}
		return operateSQLiteDataBase;
	}


	/**
	 * @author
	 */
	public void inputMyInfoToTable(LoginSuccess loginSuccessBean, String myInfoTableName,
			Context context) {
		dbHelper = new DbHelper(context);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		String sql = "CREATE TABLE IF NOT EXISTS " + myInfoTableName
				+ "(bian_hao INTEGER PRIMARY KEY AUTOINCREMENT," +
	      		"intro_Info NTEXT,ka_hao NTEXT,now_jin_bi NTEXT,jiang_jin_bi NTEXT,dai_jin_quan NTEXT);";
		db.execSQL(sql);
		if (loginSuccessBean != null) {
			ContentValues contentValues = new ContentValues();
			contentValues.put("bian_hao", loginSuccessBean.getUserId());
			contentValues.put("intro_Info", loginSuccessBean.getRecommendPerson());
			contentValues.put("ka_hao", "");
			contentValues.put("now_jin_bi", loginSuccessBean.getXianjinMoney());
			contentValues.put("jiang_jin_bi", loginSuccessBean.getJiangjinMoney());
			contentValues.put("dai_jin_quan", loginSuccessBean.getVoucherMoney());
			db.insert(myInfoTableName, null, contentValues);
		}

	}

	
	/**
	 * <p>
	 */

	/**
	 * @author 个人信息logSuccess
	 */
	public LoginSuccess getMyInfoData(String myInfoTableName,
			Context context) {
		dbHelper = new DbHelper(context);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		String[] columns = new String[6];
		columns[0] = "bian_hao";
		columns[1] = "intro_Info";
		columns[2] = "ka_hao";
		columns[3] = "now_jin_bi";
		columns[4] = "jiang_jin_bi";
		columns[5] = "dai_jin_quan";
		Cursor localCursor = db.query(myInfoTableName, columns, null, null, null,
				null, null,null);
		LoginSuccess loginSuccess = new LoginSuccess();
//		if (localCursor.moveToFirst()) {
//			do {
				loginSuccess.setUserId(localCursor.getString(localCursor
						.getColumnIndex("bian_hao")));
		        loginSuccess.setRecommendPerson(localCursor.getString(localCursor
						.getColumnIndex("intro_Info")));
		        loginSuccess.setXianjinMoney(localCursor.getString(localCursor
						.getColumnIndex("now_jin_bi")));
		        loginSuccess.setJiangjinMoney(localCursor.getString(localCursor
						.getColumnIndex("jiang_jin_bi")));
		        loginSuccess.setVoucherMoney(localCursor.getString(localCursor
						.getColumnIndex("dai_jin_quan")));

//			} while (localCursor.moveToNext());
//		}

		return loginSuccess;

	}

	
	public void deleteSongFromPlayList(Context context, String name) {
		dbHelper = new DbHelper(context);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.delete("Song_Table", "songName="+name, null);
	}


	/**
	 * 当退出程序或者不用数据库时需要关闭数据库 
	 * 该方法目前没有被调用,请在用完数据库后调用该方法
	 */
	public void closeDataBase() {
		
	}
}
